package com.epoint.mobileoa.ui.infopub;

import java.util.Date;
import java.util.HashMap;

import android.app.DatePickerDialog.OnDateSetListener;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.db.DBHelp;
import com.epoint.mobileoa.service.MainService;
import com.epoint.mobileoa.tools.AlertHelp;
import com.epoint.mobileoa.tools.ControlsHelp;
import com.epoint.mobileoa.tools.DateHelp;
import com.epoint.mobileoa.tools.ToastHelp;

/**
 * @author liyc
 * @time 2012-5-4 下午7:15:11
 * @annotation
 */
public class InformationPubView extends SuperView {

	private Spinner spXxlx, spFbbm;

	private String[] xxlx = new String[] { "新闻类型", "链接类型", "文档类型" };

	private Button btXxrq, btQrtj;

	private EditText etXxrq, etFzlm, etXxbt, etNr;

	private TextView title_tv;
	private TextView content_tv;
	private TextView lanmu_tv;
	private TextView dept_tv;
	private TextView leixing_tv;
	private TextView infoDate_tv;

	@Override
	public void initControls() {

		title_tv = (TextView) findViewById(R.id.title_tv);
		content_tv = (TextView) findViewById(R.id.content_tv);
		lanmu_tv = (TextView) findViewById(R.id.lanmu_tv);
		dept_tv = (TextView) findViewById(R.id.dept_tv);
		leixing_tv = (TextView) findViewById(R.id.leixing_tv);
		infoDate_tv = (TextView) findViewById(R.id.tvXxrq);

		title_tv.setTextSize(16);
		content_tv.setTextSize(16);
		lanmu_tv.setTextSize(16);
		dept_tv.setTextSize(16);
		leixing_tv.setTextSize(16);
		infoDate_tv.setTextSize(16);

		setTextViewFontColor(title_tv);
		setTextViewFontColor(content_tv);
		setTextViewFontColor(lanmu_tv);
		setTextViewFontColor(dept_tv);
		setTextViewFontColor(leixing_tv);
		setTextViewFontColor(infoDate_tv);

		etXxbt = (EditText) findViewById(R.id.etXxbt);
		etNr = (EditText) findViewById(R.id.etNr);
		etFzlm = (EditText) findViewById(R.id.etFzlm);
		btQrtj = (Button) findViewById(R.id.btQrtj);
		btQrtj.setOnClickListener(this);
		etXxrq = (EditText) findViewById(R.id.etXxrq);
		btXxrq = (Button) findViewById(R.id.btXxrq);
		btXxrq.setOnClickListener(this);
		spXxlx = (Spinner) findViewById(R.id.spXxlx);
		spFbbm = (Spinner) findViewById(R.id.spFbbm);
		ArrayAdapter<String> xxlxadapter = ControlsHelp.getSPAdapter(this);
		for (String s : xxlx) {
			xxlxadapter.add(s);
		}
		spXxlx.setAdapter(xxlxadapter);
		ArrayAdapter<String> fbbmadapter = ControlsHelp.getSPAdapter(this);
		fbbmadapter.add("个人名义发布");
		String ouname = DBHelp.getUserOuname(this, fp.getUserguid());
		fbbmadapter.add(ouname);
		spFbbm.setAdapter(fbbmadapter);
		etXxrq.setText(DateHelp.convertDate(new Date(), "yyyy-MM-dd"));
		etFzlm.setText("公告栏");
	}

	@Override
	public void onClick(View v) {
		if (v == btXxrq) {
			AlertHelp.showDatePickDialog(this, new OnDateSetListener() {

				@Override
				public void onDateSet(DatePicker view, int year,
						int monthOfYear, int dayOfMonth) {
					etXxrq.setText(year + "-"
							+ DateHelp.switchDay((monthOfYear + 1)) + "-"
							+ DateHelp.switchDay(dayOfMonth));
				}
			}, "日期选择");
		} else if (v == btQrtj) {
			if (ControlsHelp.Check_EditText_Blank_Warning(etXxbt, "请输入信息标题！")) {
				return;
			}
			if (ControlsHelp.Check_EditText_Blank_Warning(etNr, "请填写信息内容！")) {
				return;
			}

			showDialog_wait("正在提交...");
			HashMap<String, Object> param = new HashMap<String, Object>();

			param.put("title", etXxbt.getText().toString());
			param.put("content", etNr.getText().toString());
			param.put("infodate", etXxrq.getText().toString());
			param.put("infotype", spXxlx.getSelectedItem().toString());

			Task task = new InfoPubTask(this, param);
			MainService.addTask(task);
		}
	}

	@Override
	public void refresh(Object... param) {
		if (param[0].toString().equals(MainService.NORMAL)) {
			String bs = param[1].toString();
			if (bs.contains("<result>success</result>")) {
				ToastHelp.showToast(this, "信息发布成功");
				finish();
			} else {
				ToastHelp.showToast(this, "提交失败");
			}
		}
		m_Dialog.dismiss();
	}

	@Override
	public int getLayoutInt() {
		return R.layout.infopubview;
	}

	@Override
	public String setTitle() {
		return "信息发布";
	}

	@Override
	public void refreshForNewTask(int TaskId,Object... param) {
	}

}
